package com.huluobo.a2206douyindemo.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2206douyindemo.MainActivity;
import com.huluobo.a2206douyindemo.R;
import com.huluobo.a2206douyindemo.databinding.ActivityLoginBinding;
import com.huluobo.a2206douyindemo.register.RegisterActivity;
import com.huluobo.base.bean.Money;
import com.huluobo.base.bean.UserBeanResult;
import com.huluobo.base.db.AppDBUtils;
import com.huluobo.base.mvvm.BaseMVVMActivity;
import com.huluobo.base.net.Const;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class LoginActivity extends BaseMVVMActivity<ActivityLoginBinding, LoginViewModel> {

    @Override
    protected void initData() {
        viewModel.loginSuccess.observe(this, userBeanResult -> {
            if (userBeanResult.getCode() == 200) {
                //只有返回值的code为200的时候才是真实的成功,所有服务器的返回都会走success,但是有code不同的情况
                ToastUtils.showLong("欢迎回来:" + userBeanResult.getData().getUsername());
                SPUtils.getInstance().put(Const.PARAM_TOKEN, userBeanResult.getData().getToken());
                SPUtils.getInstance().put(Const.PARAM_USERNAME, userBeanResult.getData().getUsername());
                AppDBUtils.getInstance().getUserDao().insertUserInfo(userBeanResult.getData());
                //给用户一些余额
                Money money = new Money(10000f, userBeanResult.getData().getUsername());
                AppDBUtils.getInstance().getMoneyDao().insertUserMoney(money);

                EMClient.getInstance().login(userBeanResult.getData().getUsername(),
                        userBeanResult.getData().getPassword(), new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onError(int code, String error) {
                                ToastUtils.showLong("登录环信失败:" + error + ",code:" + code);
                            }
                        });

            } else {
                binding.password.setError(userBeanResult.getMessage());
            }

        });

        viewModel.loginFailed.observe(this, s -> {
            ToastUtils.showLong("登录失败:" + s);
        });

    }

    @Override
    protected void initView() {
        binding.btn.setOnClickListener(v -> {
            viewModel.login(
                    binding.username.getText().toString(),
                    binding.password.getText().toString()
            );
        });

        binding.loginToRegister.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });

        binding.loginHeader.headerTitle.setText("登录");
        binding.loginHeader.headerBack.setVisibility(View.GONE);
    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}